package com.website.system.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.website.common.controller.BaseController;
import com.website.common.warpper.ResultMap;
import com.website.system.model.AreaEasy;
import com.website.system.service.AreaEasyService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 区域管理控制器（核心）
 * @author 李星
 * @date 2020/10/19 12:43
 */
@Controller
@RequestMapping("/manage/areaeasy")
public class AreaEasyController extends BaseController {

    @Reference
    private AreaEasyService areaEasyService;

    /**
     * 跳转到 后台-->区域管理
     */
    @RequestMapping("")
    public String areaList() {
        return "manage/area_easy";
    }


    /**
     * 区域信息
     */
    @ResponseBody
    @RequestMapping("/areaTable")
    public ResultMap articleTable(@RequestParam Integer page, @RequestParam Integer limit) {
        ResultMap<List<AreaEasy>> resultMap = new ResultMap<>(SUCCESS_CODE, SUCCESS_MSG, null, null);
        Page<AreaEasy> pageInfo = new Page<>(page, limit);
        Wrapper<AreaEasy> wrapper = new EntityWrapper<>();
        List<AreaEasy> areaList = areaEasyService.selectPage(pageInfo, null).getRecords();
        resultMap.setCount(areaEasyService.selectCount(wrapper));
        resultMap.setData(areaList);
        return resultMap;
    }


    /**
     * 跳转到 后台-->区域管理--> 添加
     */
    @RequestMapping("/toAreaAdd")
    public String toAreaAdd(Model model) {
        return "manage/area_easyAdd";
    }

    /**
     * 保存发布
     */
    @ResponseBody
    @RequestMapping(value = "/areaAdd")
    public String areaAdd(@RequestParam String name) {
        Wrapper<AreaEasy> wrapper = new EntityWrapper<>();
        wrapper.eq("name", name);
        int i = areaEasyService.selectCount(wrapper);
        if (i == 0) {
            try {
                AreaEasy areaEasy = new AreaEasy();
                areaEasy.setName(name);
                areaEasyService.insert(areaEasy);
                return SUCCESS_MSG;
            } catch (Exception e) {
                return ERROR_MSG;
            }
        } else {
            return "禁止重复添加!!!";
        }
    }

    /**
     * 修改区域信息
     */
    @RequestMapping("/toAreaUpdate/{type}")
    public String getAssociation(Model model, @PathVariable Integer type) {
        model.addAttribute("area", areaEasyService.selectById(type));
        return "manage/area_easyUpdate";
    }

    /**
     * 保存-->区域信息修改
     */
    @ResponseBody
    @RequestMapping("/areaUpdate")
    public String associationUpdate(@RequestParam Integer id, @RequestParam String name) {
        try {
            AreaEasy area = areaEasyService.selectById(id);
            area.setName(name);
            areaEasyService.updateById(area);
            return SUCCESS_MSG;
        } catch (Exception e) {
            return ERROR_MSG;
        }
    }


}
